for i in range(a,b)/list[a,b]
Every iteration of i , i equal to next variable
2.while <condition>:
Every integration of i, programmer must change the variable
3.while 1:
<statement>
if<condition>:
break
4.tracint a program Being able to set a breakpoint where you need one is far more powerful. So we strongly encourage you to invest time in learning using to use your programming environment (PyScripter, in these notes) to full effect. print('...')
5.count digits
def num_zero_and_five_digits(n):
count =0while n >0:
digit = n % 10if digit ==0or digit ==5:
count = count + 1
n = n // 10return count
exercise
1.
n =0
a = [112,22,3,4,59,2]
for i in a:
if i % 2==1:
n = n + 1print(n)
2.
a = [1,3,4,2,5,6,7]
Sum =0for i in a :
if i % 2==0:
Sum = Sum + i
print(Sum)
3.
a = [-2,-4,25,-6,2]
Sum =0for i in a:
if i <0 :
Sum = Sum + i
print(Sum)
4.
a = ['asdfc','sdfi','sdfkj','d','asdas']
count =0for i in a:
iflen(i)==5:
count = count + 1print(count)
5.
a = [1,2,3,4,5,6,7,8,9]
Sum =0
count =0for i in a :
if i % 2==0:
count = count + 1if count !=1:
Sum = Sum + i
else:
Sum = Sum + i
print(Sum)
6.
a = ['aaa','bbb','ccc','sum','ccc']
n = 0
for i in a:
if i == 'sum':
n = n + 1
break
else:
n = n + 1
print (n)
8.
def print_mult_table(n):
for i inrange(1,n):
for j inrange(1,i+1):
print(j,"*",i,"=",i*j,end=' ')print(' ')
print_mult_table(10)
9.
def print_triangular_number(n):
a=0for i inrange(1,n+1):
a = a + i
print(i,' ',a)
print_triangular_number(5)
10.
def is_prime(n):
if n <2:
returnFalseelse:
for i inrange(2,n):
if n % i ==0:
returnTruereturnFalseprint(is_prime(19911121))
11.
importturtle
a=[160,-43,270,-43]
b=[20,10,8,12]
n=0for i in b:
n+=1turtle.left(i)turtle.forward(a[n-1])
def num_digits(n):
digit =0while1:
if -10< n <10:
return digit+1
n = n // 10
digit=digit+1print(num_digits(1000))
15.
def even_number_digit(n):
digit =0whilenot -2< n <2:
if n % 2==0and n % 10!=0:
digit +=1
n = n // 10return digit
print(even_number_digit(1230))
16
def sum_of_squares(xs):
sum=0for i in xs:
sum=sum + i**2returnsum
xs=[0]print(sum_of_squares(xs))
17.
# Your friend will complete this functiondef play_once(human_plays_first):
"""
Must play one round of the game. If the parameter
is True, the human gets to play first, else the
computer gets to play first. When the round ends,
the return value of the function is one of
-1 (human wins), 0 (game drawn), 1 (computer wins).
"""# This is all dummy scaffolding code right at the moment...importrandom# See Modules chapter ...
rng =random.Random()# Pick a random result between -1 and 1.
result = rng.randrange(-1,2)print("Human plays first={0}, winner={1} "
.format(human_plays_first, result))return result
def play_first(n):
if n % 2==0:
returnTrueprint('human first')else:
returnFalseprint('computer first')def main():
human=0
computer=0
drawn=0whileinput('do you want to play again'=='yes'):
result=play_once(play_first(n),human,drawn,computer)if result ==1:
print('you win !')
human = human + 1elif result == -1:
print('coumpter win !')
computer = computer + 1else:
print('game drawn!')
drawn = drawn + 1else:
print('your score is',human,'the win rate is',human/(human+computer+drawn))
7.9 summer vacation HW
Chapter 7
notes
1.for
for i in range(a,b)/list[a,b]Every iteration of i , i equal to next variable
2.while <condition>:
Every integration of i, programmer must change the variable
3.while 1:
<statement>
if<condition>:
break
4.tracint a program
Being able to set a breakpoint where you need one is far more powerful. So we strongly encourage you to invest time in learning using to use your programming environment (PyScripter, in these notes) to full effect.
print('...')
5.count digits
exercise
1.2.
3.
4.
5.
6.
a = ['aaa','bbb','ccc','sum','ccc'] n = 0 for i in a: if i == 'sum': n = n + 1 break else: n = n + 1 print (n)8.9.
10.
11.
12.
13.
2、5
14.
15.
16
17.